package cn.fl.seccar.controller;

import cmm.comm.facade.framework.api.ISysCtrlCfgApiService;
import cmm.comm.facade.framework.condition.SysCtrlCfgQueryCondition;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.seccar.service.IAucSplUsrService;
import cn.fl.seccar.vo.AucSplUsrVO;
import cn.fl.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSONObject;
import fl.seccar.facade.auccar.condition.AucSplUsrQC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Description: 商家用户表 控制层  
 */
@Controller
@RequestMapping("/auccar/aucsplusr")
public class AucSplUsrController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(AucSplUsrController.class);

	@Autowired
	private IAucSplUsrService aucSplUsrService;

	/**
	 * 系统配置API
	 */
	@Autowired
	private ISysCtrlCfgApiService sysCtrlCfgApiService;

	/**
	 * 控制配置代码(狮桥资产竞拍系统默认密码)
	 */
	private final static String CRTL_CODE_SECCAR_DEF_PWD = "SECCAR_DEF_PWD";

	/**
	 * 狮桥资产竞拍系统默认密码
	 */
	private final static String SECCAR_DEF_PWD = "ABcD.1119";

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("seccar/aucSplUsr/aucSplUsrList");
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toAddOrModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddOrModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("seccar/aucSplUsr/aucSplUsrAddOrModify");
		view.addObject("id", id);
		String defPassword = this.getDefPassword();
		view.addObject("defPassword", defPassword);
		BaseLogUtils.info(logger, "toAddOrModifyPage", "跳转到新增/修改二手车竞拍商户修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("seccar/aucSplUsr/aucSplUsrDetail");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toDetailPage", "跳转到详细二手车竞拍商户详细页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String splNm = request.getParameter("splNm");
		String splLnkMan = request.getParameter("splLnkMan");
		String splTel = request.getParameter("splTel");
		String usrNm = request.getParameter("usrNm");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		AucSplUsrQC qc = new AucSplUsrQC();
		if (StringUtils.isNotEmpty(splNm)) {
			qc.setSplNm(splNm);
		}
		if (StringUtils.isNotEmpty(splLnkMan)) {
			qc.setSplLnkMan(splLnkMan);
		}
		if (StringUtils.isNotEmpty(splTel)) {
			qc.setSplTel(splTel);
		}
		if(StringUtils.isNotEmpty(usrNm)){
			qc.setUsrNm(usrNm);
		}
		qc.setUsrId(CurrentThreadContext.getCurrentUserId());
		page.setCondition(qc);
		Pager pager = this.aucSplUsrService.searchListPage(page);
		List<AucSplUsrVO> list = (List<AucSplUsrVO>) pager.getResultList();
		ListPageVO<AucSplUsrVO> listPage = new ListPageVO<AucSplUsrVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询二手车竞拍商户列表数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.aucSplUsrService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			AucSplUsrVO vo = this.aucSplUsrService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/addOrModify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String splNm = request.getParameter("splNm");
		String splLnkMan = request.getParameter("splLnkMan");
		String splTel = request.getParameter("splTel");
		String bkAccNm = request.getParameter("bkAccNm");
		String bkAccNo = request.getParameter("bkAccNo");
		String bkNm = request.getParameter("bkNm");
		String bkNo = request.getParameter("bkNo");
		String splRem = request.getParameter("splRem");
		String usrNm = request.getParameter("usrNm");
		String regStdProvCd = request.getParameter("regStdProvCd");
		String regStdCityCd = request.getParameter("regStdCityCd");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		
		if(StringUtils.isEmpty(splNm)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("商户名称不能为空!");
			return result;
		}
		
		if(StringUtils.isEmpty(regStdProvCd)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("省份不能为空!");
			return result;
		}
		
		if(StringUtils.isEmpty(regStdCityCd)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("城市不能为空!");
			return result;
		}
		
		if(StringUtils.isEmpty(splLnkMan)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("联系人不能为空!");
			return result;
		}
		
		if(!StringUtils.isCustomerName(splLnkMan)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("联系人名称格式不正确!");
			return result;
		}
		
		if(StringUtils.isEmpty(usrNm)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("登录帐号不能为空!");
			return result;
		}
		
		if(!StringUtils.checkMobileNumber(usrNm)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("登录帐号格式不正确(手机号)!");
			return result;
		}
		
		AucSplUsrVO entity = null;
		try {
			entity = new AucSplUsrVO();
			entity.setBkAccNm(bkAccNm);
			entity.setBkAccNo(bkAccNo);
			entity.setBkNm(bkNm);
			entity.setBkNo(bkNo);
			entity.setCrtTm(new Date());
			entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			entity.setIsDel(BaseConstant.IS_YESNO_NO);
			entity.setIsValid(BaseConstant.IS_YESNO_YES);
			entity.setRgnCyCd(regStdCityCd);
			entity.setRgnPrCd(regStdProvCd);
			entity.setSplLnkMan(splLnkMan);
			entity.setSplNm(splNm);
			entity.setSplRem(splRem);
			entity.setSplTel(splTel);
			entity.setUsrNm(usrNm);
			if(StringUtils.isEmpty(id)){
				entity.setSysCd(CurrentThreadContext.getCurrentSysCd());
				entity.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
				entity.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
				Long mainId = this.aucSplUsrService.add(entity);
				result.setData(mainId);
			}else{
				entity.setId(Long.valueOf(id));
				boolean mdfPass = this.aucSplUsrService.modify(entity);
				if(mdfPass){
					result.setData(entity.getId());
				}else{
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("修改失败!");
					return result;
				}
			}
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName(),ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName(),ex);
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modifyIsVliad.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyIsVliad(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String isValid = request.getParameter("isValid");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modifyIsVliad", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		if(StringUtils.isEmpty(isValid)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("验证状态不能为空!");
			BaseLogUtils.info(logger, "modifyIsVliad", "修改数据,验证状态不能为空!.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		AucSplUsrVO entity = null;
		try {
			entity = new AucSplUsrVO();
			entity.setId(Long.valueOf(id));
			entity.setIsValid(Integer.valueOf(isValid));
			boolean success = this.aucSplUsrService.modifyIsVliad(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("操作失败!");
				BaseLogUtils.info(logger, "modifyIsVliad", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modifyIsVliad", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modifyIsVliad", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "modifyIsVliad", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 获取狮桥资产竞拍系统默认密码
	 * @return
	 */
	private String getDefPassword(){
		SysCtrlCfgQueryCondition qc = new SysCtrlCfgQueryCondition();
		qc.setSysCd(WebBaseConstant.SYS_SYS_CD);
		qc.setOrgCd(WebBaseConstant.SYS_P_ORG_CD);
		qc.setCtrlCode(CRTL_CODE_SECCAR_DEF_PWD);
		String value = sysCtrlCfgApiService.searchSysCtrlCfgByCtrlCode(qc);
		if(StringUtils.isEmpty(value)){
			value = SECCAR_DEF_PWD;
		}
		return value;
	}
}

